package main

import (
	"bytes"
	"fmt"
	"math"
	"net/http"
	"strings"
	"unicode/utf8"
)

func test() {
	var x uint8 = 1 << 1 | 1 << 5
	var y uint8 = 1 << 1 | 1 << 2
	fmt.Printf("%08b\n", x) // "00100010", the set {1, 5}
	fmt.Printf("%08b\n", y) // "00000110", the set {1, 2}

	fmt.Printf("%08b\n", x&y)  // "00000010", the intersection {1}
	fmt.Printf("%08b\n", x|y)  // "00100110", the union {1, 2, 5}
	fmt.Printf("%08b\n", x^y)  // "00100100", the symmetric difference {2, 5}
	fmt.Printf("%08b\n", x&^y) // "00100000", the difference {5}

	o := 0666
	fmt.Printf("%d %[1]o %#[1]o\n", o) // "438 666 0666"
}

func floattest() {
	var f float32 = 1 << 24
	fmt.Println(f == f + 1)			//true

	for x := 0; x < 8; x++ {
		fmt.Printf("x = %d e^x = %8.3f\n", x, math.Exp(float64(x)))
	}
	fmt.Printf("------------------------------------\n")
	for x := 0; x < 8; x++ {
		fmt.Printf("x = %d e^x = %8.3g\n", x, math.Exp(float64(x)))
	}

	var z float64
	fmt.Println(z, -z, 1/z, -1/z, z/z)

	nan := math.NaN()
	fmt.Println(nan == nan, nan < nan, nan > nan)
}

const (
	width, height = 600, 320            // canvas size in pixels
	cells         = 100                 // number of grid cells
	xyrange       = 30.0                // axis ranges (-xyrange..+xyrange)
	xyscale       = width / 2 / xyrange // pixels per x or y unit
	zscale        = height * 0.4        // pixels per z unit
	angle         = math.Pi / 6         // angle of x, y axes (=30°)
)

var sin30, cos30 = math.Sin(angle), math.Cos(angle) // sin(30°), cos(30°)

func f(x, y float64) float64 {
	r := math.Hypot(x, y) // distance from (0,0)
	return math.Sin(r) / r
}

func corner(i, j int) (float64, float64) {
	// Find point (x,y) at corner of cell (i,j).
	x := xyrange * (float64(i)/cells - 0.5)
	y := xyrange * (float64(j)/cells - 0.5)

	// Compute surface height z.
	z := f(x, y)

	// Project (x,y,z) isometrically onto 2-D SVG canvas (sx,sy).
	sx := width/2 + (x-y)*cos30*xyscale
	sy := height/2 + (x+y)*sin30*xyscale - z*zscale
	return sx, sy
}

func surface(out http.ResponseWriter, width int64, height int64, color string) {
	out.Header().Set("Content-Type", "image/svg+xml")
	fmt.Fprintf(out, "<svg xmlns='http://www.w3.org/2000/svg'"+
		" style='stroke: %s;fill:white;stroke-width:0.7'"+
		" width='%d' height='%d'>", color, width, height)
	for i := 0; i < cells; i++ {
		for j := 0; j < cells; j++ {
			ax, ay := corner(i+1, j)
			bx, by := corner(i, j)
			cx, cy := corner(i, j+1)
			dx, dy := corner(i+1, j+1)
			fmt.Fprintf(out, "<polygon points='%g,%g %g,%g %g,%g %g,%g'/>\n",
				ax, ay, bx, by, cx, cy, dx, dy)
		}
	}
	fmt.Fprintf(out, "</svg>")
}

func strtest() {
	s := "Hello, 世界"
	fmt.Println(len(s))
	fmt.Println(utf8.RuneCountInString(s))
	for i:=0;i<len(s);{
		r, size := utf8.DecodeRuneInString(s[i:])
		fmt.Printf("%d\t%c\n", i, r)
		i += size
	}

	fmt.Println(basename1("a/b/c.go"))
	fmt.Println(basename1("c.d.go"))
	fmt.Println(basename1("abc"))
	fmt.Println("-----------------------------------------------")
	fmt.Println(basename2("a/b/c.go"))
	fmt.Println(basename2("c.d.go"))
	fmt.Println(basename2("abc"))

	fmt.Println("-----------------------------------------------")
	s = "12345"
	fmt.Println(comma(s))
	fmt.Println("-----------------------------------------------")
	fmt.Println(intsToString([]int{1, 2, 3, 4}))
	fmt.Println("-----------------------------------------------")
	fmt.Println(a ,b, c, d)
	fmt.Println("-----------------------------------------------")
	fmt.Println(Sunday ,Monday, TuesDay, Wednesday, Thursday, Friday, Saturday)
	fmt.Println("-----------------------------------------------")
	fmt.Println(FlagUp ,FlagBroadcast, FlagLoopback, FlagPointToPoint, FlagMulticast)
}

const (
	a = 1
	b
	c = 2
	d
)

type Weekday int
const(
	Sunday Weekday = iota
	Monday
	TuesDay
	Wednesday
	Thursday
	Friday
	Saturday
)

type Flags uint

const (
	FlagUp Flags = 1 << iota // is up
	FlagBroadcast            // supports broadcast access capability
	FlagLoopback             // is a loopback interface
	FlagPointToPoint         // belongs to a point-to-point link
	FlagMulticast            // supports multicast access capability
)

func basename1(s string)string {
	for i:=len(s) - 1; i >= 0; i--{
		if s[i] == '/'{
			s = s[i+1:]
			break;
		}
	}
	for i:=len(s) - 1; i >= 0;i--{
		if s[i] == '.'{
			s = s[:i]
			break
		}
	}

	return s
}

func basename2(s string) string {
	slash := strings.LastIndex(s, "/")
	s = s[slash+1:]
	if dot := strings.LastIndex(s, "."); dot >= 0{
		s = s[:dot]
	}

	return s
}

func comma(s string) string {
	n := len(s)
	if n <= 3{
		return s
	}

	return comma(s[:n-3])+","+ s[n-3:]
}

func intsToString(values []int) string {
	var buf bytes.Buffer
	buf.WriteByte('[')
	for i, v := range values{
		if i > 0{
			buf.WriteByte(',')
		}
		fmt.Fprintf(&buf, "%d", v)
	}
	buf.WriteByte(']')
	return buf.String()
}

func main() {
	//test()
	//floattest()
	//surface()

	//http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	//	width := r.FormValue("width")
	//	widthInt, err := strconv.ParseInt(width, 10, 64)
	//	if err != nil{
	//		w.Header().Set("Content-Type", "text/html; charset=utf-8")
	//		fmt.Fprintf(w, "<html><body>You need a param 'width' and that must be  a number, you can visit like<a href=\"http://localhost:8888/?width=2160&height=1280&color=red\">http://localhost:8888/?width=2160&height=1280&color=red</a></body></html>")
	//		return
	//	}
	//	height := r.FormValue("height")
	//	heightInt, err := strconv.ParseInt(height, 10, 64)
	//	if err != nil {
	//		w.Header().Set("Content-Type", "text/html; charset=utf-8")
	//		fmt.Fprintf(w, "<html><body>You need a param 'height' and that must be  a number, you can visit like<a href=\"http://localhost:8888/?width=2160&height=1280&color=red\">http://localhost:8888/?width=2160&height=1280&color=red</a></body></html>")
	//		return
	//	}
	//	color := r.FormValue("color")
	//	surface(w, widthInt, heightInt, color)
	//})
	//log.Fatal(http.ListenAndServe("localhost:8888", nil))

	strtest()
	s := []int{0, 1, 2, 3, 4, 5}
	fmt.Println(s)
	s = s[:0]
	fmt.Println("-------------------------")
	fmt.Println(s)
}
